package com.example.biye;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.format.DateFormat;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;

import com.joybar.librarycalendar.data.CalendarDate;
import com.joybar.librarycalendar.fragment.CalendarViewFragment;
import com.joybar.librarycalendar.fragment.CalendarViewPagerFragment;

import java.sql.Time;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;


public class SecondActivity extends AppCompatActivity implements
        CalendarViewPagerFragment.OnPageChangeListener,
        CalendarViewFragment.OnDateClickListener,
        CalendarViewFragment.OnDateCancelListener {
    private TextView time;
    private TextView timean;
    private TextView timeyq;
    private TextView timedate;
    private boolean isChoiceModelSingle = false;
    private List<CalendarDate> mListDate = new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getSupportActionBar().hide();
        setContentView(R.layout.activity_second);
        time = (TextView) findViewById(R.id.test_time);
        timean = (TextView) findViewById(R.id.test_timean);
        timeyq = (TextView) findViewById(R.id.test_timeyq);
        timedate = (TextView) findViewById(R.id.tv_date);
        new TimeThread().start();

        new Timer().schedule(new TimerTask() {
            @Override
            public void run() {
                mHandler.sendEmptyMessage(1);
            }
        }, 0, 1000);
        Button button2 = (Button) findViewById(R.id.button2);
        button2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(SecondActivity.this, TabHostActivity.class);
                startActivity(intent);
            }
        });
        initFragment();
    }


    private void initFragment() {
        FragmentManager fm = getSupportFragmentManager();
        FragmentTransaction tx = fm.beginTransaction();
        Fragment fragment = CalendarViewPagerFragment.newInstance(isChoiceModelSingle);
        tx.replace(R.id.fl_content, fragment);
        tx.commit();
    }

    @Override
    public void onDateCancel(CalendarDate calendarDate) {
        int year = calendarDate.getSolar().solarYear;
        int month = calendarDate.getSolar().solarMonth;
        int day = calendarDate.getSolar().solarDay;
        if (isChoiceModelSingle) {
            //tv_date.setText(year + "年" + month + "月" + day + "日");
        } else {
            //System.out.println(calendarDate.getSolar().solarDay);
           mListDate.add(calendarDate);
           //tv_date.setText(listToString(mListDate));
        }
    }

    @Override
    public void onDateClick(CalendarDate calendarDate) {
        int year = calendarDate.getSolar().solarYear;
        int month = calendarDate.getSolar().solarMonth;
        int day = calendarDate.getSolar().solarDay;
        if (isChoiceModelSingle) {
           // tv_date.setText(year + "年" + month + "月" + day + "日");
        } else {
            //System.out.println(calendarDate.getSolar().solarDay);
            mListDate.add(calendarDate);
            //tv_date.setText(listToString(mListDate));
        }
    }

    private static String listToString(List<CalendarDate> list) {
        StringBuffer stringBuffer = new StringBuffer();
        for (CalendarDate date : list) {
            stringBuffer.append(date.getSolar().solarYear + "-" + date.getSolar().solarMonth + "-" + date.getSolar().solarDay).append(" ");
        }
        return stringBuffer.toString();
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        return super.onCreateOptionsMenu(menu);
    }


    @Override
    public boolean onOptionsItemSelected(@NonNull MenuItem item) {
        switch (item.getItemId()) {
            case R.id.menu_single:
                isChoiceModelSingle = true;
                initFragment();
                break;
            case R.id.menu_multi:
                isChoiceModelSingle = false;
                initFragment();
                break;
            default:
                break;
        }
        return true;
    }

    @Override
    public void onPageChange(int year, int month) {
        timedate.setText(year + "年" + month + "月");
        mListDate.clear();
    }

    @Override
    public void onPointerCaptureChanged(boolean hasCapture) {
        super.onPointerCaptureChanged(hasCapture);
    }

    class TimeThread extends Thread {
        @Override
        public void run() {
            do {
                try {
                    Thread.sleep(1000);
                    Message msg = new Message();
                    msg.what = 1;  //消息(一个整型值)
                    mHandler.sendMessage(msg);// 每隔1秒发送一个msg给mHandler
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            } while (true);
        }
    }


    private Handler mHandler = new Handler() {
        @SuppressLint("HandlerLeak")
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case 1:
                    long sysTime = System.currentTimeMillis();//获取系统时间
                    CharSequence sysTimeStr = DateFormat.format("yyyy年MM月dd日 HH:mm:ss", sysTime);//时间显示格式
                    time.setText(sysTimeStr);
                    SimpleDateFormat formatter = new SimpleDateFormat("HH");
                    String currentHour = formatter.format(new Date());
                    Calendar calendar = Calendar.getInstance();
                    int hours = calendar.get(Calendar.HOUR_OF_DAY);
                    // Log.i("hour2", String.valueOf(currentHour));
                    switch (hours) {

                        case 0:
                            timean.setText("子（zi）时");
                            timeyq.setText("胆经主时，此时入睡对防治肝胆疾病和养护阳气重要");
                            break;
                        case 1:
                            timean.setText("丑（chou）时");
                            timeyq.setText("肝经主时，此时睡眠良好方能养肝，否则易出现急躁易怒,神经衰弱");
                            break;
                        case 2:
                            timean.setText("丑（chou）时");
                            timeyq.setText("肝经主时，此时睡眠良好方能养肝，否则易出现急躁易怒,神经衰弱");
                            break;
                        case 3:
                            timean.setText("寅（yin）时");
                            timeyq.setText("肺经主时，肺主气，朝百脉，深度睡眠有助于养肺");
                            break;
                        case 4:
                            timean.setText("寅（yin）时");
                            timeyq.setText("肺经主时，肺主气，朝百脉，深度睡眠有助于养肺");
                            break;
                        case 5:
                            timean.setText("卯（mao）时");
                            timeyq.setText("大肠经主时，晨起适量饮水，有助大肠传导功能，防止便秘");
                            break;
                        case 6:
                            timean.setText("卯（mao）时");
                            timeyq.setText("大肠经主时.晨起适量饮水，有助大肠传导功能，防止便秘");
                            break;
                        case 7:
                            timean.setText("辰（chen）时");
                            timeyq.setText("胃经主时，此时早餐，营养易于消化吸收");
                            break;
                        case 8:
                            timean.setText("辰（chen）时");
                            timeyq.setText("胃经主时，此时早餐，营养易于消化吸收");
                            break;
                        case 9:
                            timean.setText("巳（si）时");
                            timeyq.setText("脾经主时，脾主四肢，适当运动，有助于脾的运化水谷，升清降浊");
                            break;
                        case 10:
                            timean.setText("巳（si）时");
                            timeyq.setText("脾经主时，脾主四肢，适当运动，有助于脾的运化水谷，升清降浊");
                            break;
                        case 11:
                            timean.setText("午（wu）时");
                            timeyq.setText("心经主时，餐后午休，有利于养心安神，保证午后精力充沛");
                            break;
                        case 12:
                            timean.setText("午（wu）时");
                            timeyq.setText("心经主时，餐后午休，有利于养心安神，保证午后精力充沛");
                            break;
                        case 13:
                            timean.setText("未（wei）时");
                            timeyq.setText("小肠经主时，宜在此前午餐，以利于小肠受盛化物");
                            break;
                        case 14:
                            timean.setText("未（wei）时");
                            timeyq.setText("小肠经主时，宜在此前午餐，以利于小肠受盛化物");
                            break;
                        case 15:
                            timean.setText("申（shen）时");
                            timeyq.setText("膀胱经主时，此时多喝水，利于代谢产物通过小便排泄");
                            break;
                        case 16:
                            timean.setText("申（shen）时");
                            timeyq.setText("膀胱经主时，此时多喝水，利于代谢产物通过小便排泄");
                            break;
                        case 17:
                            timean.setText("酉（you）时");
                            timeyq.setText("肾经主时，宜按时晚餐，另需防过劳，以助藏精");
                            break;
                        case 18:
                            timean.setText("酉（you）时");
                            timeyq.setText("肾经主时，宜按时晚餐，另需防过劳，以助藏精");
                            break;
                        case 19:
                            timean.setText("戌（xu）时");
                            timeyq.setText("心包经主时，此时适量运动或娱乐，有助于气血运行，怡养心神和养护心气");
                            break;
                        case 20:
                            timean.setText("戌（xu）时");
                            timeyq.setText("心包经主时，此时适量运动或娱乐，有助于气血运行，怡养心神和养护心气");
                            break;
                        case 21:
                            timean.setText("亥（hai）时");
                            timeyq.setText("三焦经主时，此时洗漱宽衣准备入睡，有助于水谷，诸气运行，阴阳和合和养颜益寿");
                            break;
                        case 22:
                            timean.setText("亥（hai）时");
                            timeyq.setText("三焦经主时，此时洗漱宽衣准备入睡，有助于水谷，诸气运行，阴阳和合和养颜益寿");
                            break;
                        case 23:
                            timean.setText("子（zi）时");
                            timeyq.setText("胆经主时，此时入睡对防治肝胆疾病和养护阳气重要");
                            break;
                    }
                default:
                    break;
            }


        }
    };

}




